<?php

namespace Modules\Production\Repository;

use Illuminate\Support\Facades\DB;
use Modules\Production\Models\PickOrder;
use Modules\Production\Models\PickOrderDetail;

class PickOrderRepository
{

    /**
     * 根据箱号查询所有领料单
     * @param $lotnum
     */
    static public function getPickOrderByLotNum($lotnum){

        return PickOrderDetail::where('lot_num',$lotnum)->with(['order','material'])->get();

    }

    /**
     * 根据id查询领料单子项并返回订单头
     * @param $detailId
     */
    static public function getPickDetailWithOrderById($detailId){
        return PickOrderDetail::where('id',$detailId)->with('order')->first();
    }

    /**
     * 根据领料单id汇总子项
     * @param $orderId
     * @return mixed
     */
    static public function getGroupByListByOrderId($orderId){
        return PickOrderDetail::select('production_no','fid','fentryid','material_no','row_num','unit','wms_code',DB::raw('sum(qty) as qty'))
            ->where('order_id',$orderId)
            ->groupBy('production_no','fid','fentryid','material_no','row_num','unit','wms_code')
            ->get();
    }

    /**
     * 根据生产领料单号查询领料单据头信息
     * @param $pickNo
     */
    static public function getProductionNoByPickNo($pickNo){
        return PickOrder::where('docno',$pickNo)->first();
    }

    /**
     * 根据领料单id和品号获取已领料数量
     * @param $pickId
     * @param $materialNo
     */
    static public function getPickCountByPickIdAndMaterialNo($pickId,$materialNo){
        return PickOrderDetail::where('order_id',$pickId)->where('material_no',$materialNo)->sum('qty');
    }

    /**
     * 领料子项增加拆箱数量
     * @param $pickDetailId
     * @param $qty
     * @return mixed
     */
    static public function incSplitQty($pickDetailId,$qty){
        return PickOrderDetail::where('id',$pickDetailId)->increment('split_qty',$qty);
    }

}
